package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VideoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ManageService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private VideoApi videoApi;

    @DubboReference
    private MovementApi movementApi;

    //分页查询所有的数据
    public PageResult findAllUsers(int page, int pagesize) {
        //1、调用API分页查询数据列表  IPage
        IPage<UserInfo> iPage = userInfoApi.findAll(page,pagesize);
        //2、转化为PageResult
        return new PageResult(page,pagesize,iPage.getTotal(),iPage.getRecords());
    }



    //根据id查询用户资料
    public UserInfo findUserById(Long id) {
        return userInfoApi.findById(id);
    }





    //查询视频分页列表
    public PageResult findVideosByUserId(Long uid, int page, int pagesize) {
        //1、调用videoApi服务，查询分页视频列表 ：PageResult -- List<Video>
        PageResult result = videoApi.findByUserId(uid,page,pagesize);
        //2、获取PageResult中的数据集合List<Video>
        List<Video> items = (List<Video>) result.getItems();
        if(CollUtil.isEmpty(items)) {
            return result;
        }
        //3、一个Video构造一个VideoVo   List<VideoVo>
        List<Long> userIds = CollUtil.getFieldValues(items, "userId",
                Long.class); //所有视频作者id
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, null);//所有视频作者对象
        List<VideoVo> vos = new ArrayList<>();
        for (Video video : items) {
            //通过视频作者的id从map集合中获取，当前视频作者信息
            UserInfo userInfo = map.get(video.getUserId());
            if(userInfo != null) {
                vos.add(VideoVo.init(userInfo,video));
            }
        }
        //4、替换PageResult中的数据集合
        result.setItems(vos);
        return result;
    }

    //分页查询动态列表 movementvo
    public PageResult findMovementByUserId(Long uid, Integer state, int page, int pagesize) {
        //1、调用API分页查询  PageResult -- List<Movement>
        PageResult result = movementApi.findMovement(uid,state,page,pagesize);
        //2、获取PageResult中的数据集合List<Movement>
        List<Movement> items = (List<Movement>) result.getItems();
        if(CollUtil.isEmpty(items)) {
            return result;
        }
        //3、一个Movement构造一个MovementVo   List<Movement>
        List<Long> userIds = CollUtil.getFieldValues(items, "userId", Long.class); //所有视频作者id
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, null);//所有视频作者对象
        List<MovementsVo> vos = new ArrayList<>();
        for (Movement movement : items) {
            UserInfo userInfo = map.get(movement.getUserId());
            if(userInfo != null) {
                vos.add(MovementsVo.init(userInfo,movement));
            }
        }
        //4、替换PageResult中的数据集合
        result.setItems(vos);
        return result;
    }
}
